package model.forview;

import java.util.ArrayList;

import model.wrap.structure.Structure;
import model.wrap.type.Type;
import models.ConstantName;
import models.ConstantValue;

public class NodeCreateModelForView extends ContentForMainTemplate
{
    protected ArrayList<Type> types;
    protected ArrayList<Structure> structures;
    protected ArrayList<Boolean> isSelected;
    protected Long selectedStructureId;

    public Long typeId;
    public Long structureId;
    public String title;

    public final String typeIdName = "typeId";
    public final String structureIdName = "structureId";
    public final String titleName = "title";

    public String saveButton;
    public String cancelButton;

    public String typeLabel;
    public String structureLabel;
    public String titleLabel;

    public NodeCreateModelForView()
    {
	this.selectedStructureId = new Long(-1);

	setDefaultValues();
	fillArrays(this.selectedStructureId);
    }

    public NodeCreateModelForView(Long selectedStructureId)
    {
	this.selectedStructureId = selectedStructureId;

	setDefaultValues();
	fillArrays(this.selectedStructureId);
    }

    protected void setDefaultValues()
    {
	String title = ConstantValue.getConstantValue(ConstantName.NODE_CREATE_PAGE_TITLE);
	String header = ConstantValue.getConstantValue(ConstantName.NODE_CREATE_PAGE_HEADER);
	String footer = ConstantValue.getConstantValue(ConstantName.NODE_CREATE_PAGE_FOOTER);
	String userName = "";

	title = title != null ? title : "Создание узла";
	header = header != null ? header : "Создание узла";
	footer = footer != null ? footer : "Создание узла";

	titleLabel = ConstantValue.getConstantValue(ConstantName.NODE_CREATE_TITLE_LABEL);
	typeLabel = ConstantValue.getConstantValue(ConstantName.NODE_CREATE_TYPE_LABEL);
	structureLabel = ConstantValue.getConstantValue(ConstantName.NODE_CREATE_STRUCTURE_LABEL);
	saveButton = ConstantValue.getConstantValue(ConstantName.SAVE_BUTTON);
	cancelButton = ConstantValue.getConstantValue(ConstantName.CANCEL_BUTTON);

	titleLabel = titleLabel != null ? titleLabel : "Создание узла";
	typeLabel = typeLabel != null ? typeLabel : "Type";
	structureLabel = structureLabel != null ? structureLabel : "Structure";
	saveButton = saveButton != null ? saveButton : "Save";
	cancelButton = cancelButton != null ? cancelButton : "Cancel";

	mainModelForView = new MainModelForView(title, userName, header, footer);
    }

    private void fillArrays(Long selectValue)
    {
	types = Type.getAllTypes();
	structures = Structure.getAllStructures();

	boolean defaultSelect = true;
	this.isSelected = new ArrayList<Boolean>();
	for (int i = 0; i < this.getStructuresSize(); i++)
	{
	    if (this.getStructuresId(i).equals(selectValue) || (defaultSelect == true && i + 1 == this.getStructuresSize()))
	    {
		defaultSelect = false;
		this.isSelected.add(new Boolean(true));
	    }
	    else
	    {
		this.isSelected.add(new Boolean(false));
	    }
	}
    }

    public int getTypesSize()
    {
	return types.size();
    }

    public Long getTypesId(int index)
    {
	return types.get(index).getID();
    }

    public String getTypesTitle(int index)
    {
	return types.get(index).getTitle();
    }

    public int getStructuresSize()
    {
	return structures.size();
    }

    public Long getStructuresId(int index)
    {
	return structures.get(index).getId();
    }

    public String getStructuresTitle(int index)
    {
	return structures.get(index).getTitle();
    }

    public boolean isStructureSelected(int index)
    {
	return this.isSelected.get(index);
    }

}
